package com.tanhua.dubbo.api;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.mg.Friend;
import com.tanhua.domain.mg.Publish;
import com.tanhua.domain.mg.TodayBeautyMG;
import com.tanhua.domain.mg.Visitor;
import com.tanhua.domain.vo.UserLike;
import com.tanhua.dubbo.mg.UserLikeNumApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserLikeNumApiImpl implements UserLikeNumApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 删除喜欢的关系
     * @param currentUserId
     * @param fansId
     */
    @Override
    public void deleteRelation(Long currentUserId, Long fansId) {
        //创建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(currentUserId).and("userId").is(fansId));
        //查询
        mongoTemplate.remove(query,UserLike.class);
    }

    /**
     * 根据当前id查询我关注的对象
     *
     * @param currentUserId
     * @return
     */
    @Override
    public Integer countMyLike(Long currentUserId) {
        //创建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        //查询
        long count = mongoTemplate.count(query, UserLike.class);
        return (int) count;
    }

    /**
     * 根据当前id查询喜欢我的用户
     *
     * @param currentUserId
     * @return
     */
    @Override
    public Integer countLikeMe(Long currentUserId) {
        //创建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(currentUserId));
        //查询
        long count = mongoTemplate.count(query, UserLike.class);
        return (int) count;
    }

    /**
     * 互相喜欢 翻页列表
     */
    @Override
    public PageResult<TodayBeautyMG> findFriendScoreAndData(Long currentUserId, Integer page, Integer pagesize) {
        //a.设置分页条件
        Query query = new Query();
        query.limit(pagesize).skip((page - 1) * pagesize);
        //b.设置查询条件
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        //c.使用mongoTemplate查询 返回List<T>类型
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //d.创建一个List集合 存储Recommend
        List<TodayBeautyMG> recommendList=new ArrayList<>();
        //e.非空判断 遍历list
        if (CollectionUtils.isNotEmpty(friends)) {
            for (Friend friend : friends) {
                //f.根据集合对象中的2个ID 查询recommend_quanzi表
                int currentId = friend.getUserId().intValue();
                int otherSideId = friend.getFriendId().intValue();
                TodayBeautyMG todayBeautyMG = findRecommendByTwoId(currentId, otherSideId);
                //添加对象到recommendList集合
                recommendList.add(todayBeautyMG);
            }
        }
        //调用方法 传入参数返回pageResult
        return getTodayBeautyMGPageResult(recommendList,query,page,pagesize);
    }

    /**
     * 我喜欢的 翻页列表
     */
    @Override
    public PageResult<TodayBeautyMG> findMyLikeScoreAndData(Long currentUserId, Integer page, Integer pagesize) {
        //a.设置分页条件
        Query query = new Query();
        query.limit(pagesize).skip((page - 1) * pagesize);
        //b.设置查询条件
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        //c.使用mongoTemplate查询 返回List<T>类型
        List<UserLike> myLikes = mongoTemplate.find(query, UserLike.class);
        //d.创建一个存储RecommendQuanZi集合对象
        List<TodayBeautyMG> recommendList = new ArrayList<>();
        //e.非空判断 遍历list
        if (CollectionUtils.isNotEmpty(myLikes)) {
            for (UserLike userLike : myLikes) {
                //f.根据集合对象中的2个ID 查询recommend_quanzi表
                Long userId = userLike.getUserId();
                Long likeUserId = userLike.getLikeUserId();
                TodayBeautyMG todayBeautyMG = findRecommendByTwoId(userId.intValue(), likeUserId.intValue());
                //添加对象到recommendList集合
                recommendList.add(todayBeautyMG);
            }
        }
        //调用方法 传入参数返回pageResult
        return getTodayBeautyMGPageResult(recommendList,query,page,pagesize);
    }

    /**
     * 喜欢我的 - 翻页列表
     */
    @Override
    public PageResult<TodayBeautyMG> findLikeMeScoreAndData(Long currentUserId, Integer page, Integer pagesize) {
        //a.设置分页条件
        Query query = new Query();
        query.limit(pagesize).skip((page - 1) * pagesize);
        //b.设置查询条件
        query.addCriteria(Criteria.where("likeUserId").is(currentUserId));
        //c.使用mongoTemplate查询 返回List<T>类型
        List<UserLike> myLikes = mongoTemplate.find(query, UserLike.class);
        //d.创建一个存储RecommendQuanZi集合对象
        List<TodayBeautyMG> recommendList = new ArrayList<>();
        //e.非空判断 遍历list
        if (CollectionUtils.isNotEmpty(myLikes)) {
            for (UserLike userLike : myLikes) {
                //f.根据集合对象中的2个ID 查询recommend_quanzi表
                Long userId = userLike.getUserId();
                Long likeUserId = userLike.getLikeUserId();
                TodayBeautyMG todayBeautyMG = findRecommendByTwoId(likeUserId.intValue(), userId.intValue());
                //添加对象到recommendList集合
                recommendList.add(todayBeautyMG);
            }
        }
        //调用方法 传入参数返回pageResult
        return getTodayBeautyMGPageResult(recommendList,query,page,pagesize);
    }

    /**
     * 谁看过我 - 翻页列表
     */
    @Override
    public PageResult<TodayBeautyMG> findVisitorMeScoreAndData(Long currentUserId, Integer page, Integer pagesize) {
        //a.设置分页条件
        Query query = new Query();
        query.limit(pagesize).skip((page - 1) * pagesize);
        //b.设置查询条件
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        //c.使用mongoTemplate查询 返回List<T>类型
        List<Visitor> myLikes = mongoTemplate.find(query, Visitor.class);
        //d.创建一个存储RecommendQuanZi集合对象
        List<TodayBeautyMG> recommendList = new ArrayList<>();
        //e.非空判断 遍历list
        if (CollectionUtils.isNotEmpty(myLikes)) {
            for (Visitor visitor : myLikes) {
                //f.根据集合对象中的2个ID 查询recommend_quanzi表
                Integer userId = visitor.getUserId();
                Integer visitorUserId = visitor.getVisitorUserId();
                TodayBeautyMG todayBeautyMG = findRecommendByTwoId(userId, visitorUserId);
                //添加对象到recommendList集合
                recommendList.add(todayBeautyMG);
            }
        }
        //调用方法 传入参数返回pageResult
        return getTodayBeautyMGPageResult(recommendList,query,page,pagesize);
    }


    /**
     * 根据当前用户Id 分页条件数据、查询的类型 抽取公共方法
     * @param query
     * @param page
     * @param pagesize
     * @return
     */
    private PageResult<TodayBeautyMG> getTodayBeautyMGPageResult(List<TodayBeautyMG> recommendList,Query query,Integer page, Integer pagesize) {
        //查询总记录数
        long count = mongoTemplate.count(query, Friend.class);
        //查询总页数
        long pages = (count / pagesize) + (count % pagesize > 0 ? 1 : 0);
        //创建分页对象返回
        PageResult<TodayBeautyMG> tbMGPageResult = new PageResult<>();
        tbMGPageResult.setItems(recommendList);
        tbMGPageResult.setCounts((int) count);
        tbMGPageResult.setPages((int) pages);
        tbMGPageResult.setPage(page);
        tbMGPageResult.setPagesize(pagesize);
        return tbMGPageResult;
    }

    /**
     * 根据当前用户id 和 对方id 查询佳人信息缘分值等
     * @param currentUserId
     * @param otherSideId
     * @return
     */
    public TodayBeautyMG findRecommendByTwoId(Integer currentUserId, Integer otherSideId) {
        //创建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("userId")
                .is(otherSideId)
                .and("toUserId")
                .is(currentUserId));
        //查询
        TodayBeautyMG todayBeautyMG = mongoTemplate.findOne(query, TodayBeautyMG.class);
        if (todayBeautyMG == null) {
            //如果对象为null 那么就补全信息返回数据
            Double score = Math.random() * 90;
            todayBeautyMG = new TodayBeautyMG();
            todayBeautyMG.setToUserId(currentUserId.longValue());
            todayBeautyMG.setUserId(otherSideId.longValue());
            todayBeautyMG.setScore(score);
        }
        return todayBeautyMG;
    }
}
